var COLUMNS_WITHOUT_FORMATTER = ['Company', 'Technology'];

function load_info(){return true};

jQuery.fn.outerHTML = function(s) {
    return (s)
    ? this.before(s).remove()
    : jQuery("<p>").append(this.eq(0).clone()).html();
    }
/* begin: ListFormatter object formats data as unordered list */
var ListFormatter = function(elCell, oRecord, oColumn, oData) {

    if ((typeof(oData)=="string") && (oData.length>0)) {
        
        var lis = jQuery(oData).children('li')
        
        if (lis.length>3) {
            lis.splice(3,0,jQuery('<li><a href="#">Show ' + lis.length + ' more</a></li>'));
            lis[3] = jQuery(lis[3]).addClass('edge')[0]
            for (var n=4; n<lis.length; n++) {
                lis[n] = jQuery(lis[n]).addClass('after_edge').css({'display': 'none'})[0]
                }
            }                

        elCell.innerHTML = '<ul class="list_formatter">' + $('<div>').append(lis.clone()).remove().html() + '</ul>'
        };
    }

/* When user click on "Show more" link in unordered lists */
jQuery('li.edge').live('click', function() {
    var result = '<ul style="margin: 2em;">'
    $(this).siblings('li').each(function(){
        result += '<li>' + $(this).html() + '</li>';
        });
    result += '</ul>';
    jQuery.facebox(result);
    });      
/* end: ListFormatter object formats data as unordered list */

jQuery(document).ready(function() {
    jQuery('.no_display').hide();
    jQuery('table.scrollable').each(function() {
        if (jQuery(this).height()>400) {
            jQuery(this).wrap("<div style=\"width: 677px; height: 400px; padding: 1px; overflow-x:hidden; overflow-y:scroll;\"></div>");
            }
        });

var dsLocalTable, columnDefs;
jQuery('table.company_search_results').each(function(){
    
    var table_id = jQuery(this).attr('id');
    var placeholder_id = "tc_"+table_id;
            
    dsLocalTable = new YAHOO.util.LocalDataSource(YAHOO.util.Dom.get(table_id));
    dsLocalTable.responseType = YAHOO.util.DataSource.TYPE_HTMLTABLE;
    dsLocalTable.responseSchema = {
        fields: new Array(),
        };

    columnDefs = new Array()
    
    jQuery('#' + table_id + ' thead tr th').each(function(){
        
        var t = false;
        for (i in COLUMNS_WITHOUT_FORMATTER){
            if (COLUMNS_WITHOUT_FORMATTER[i]==jQuery(this).text()) {
                t = COLUMNS_WITHOUT_FORMATTER[i];
            };
        };
        if (t){
            columnDefs.push({key: t, label: t, width: 149 })
        } else {
            columnDefs.push(
                {key: jQuery(this).text().replace(/\s/g, ''), label: jQuery(this).text(), width: 149,  formatter: ListFormatter}
            )
        };
        dsLocalTable.responseSchema.fields.push({key: jQuery(this).text().replace(' ', '')});
    });

    if (jQuery('#'+placeholder_id).length==0) {
        return true
        }
    else {            
        jQuery('#'+table_id).hide();
        ResultsDataTableDeferred = new YAHOO.widget.ScrollingDataTable(
            placeholder_id,
            columnDefs,
            dsLocalTable,
            {
                paginator: new YAHOO.widget.Paginator({ 
                rowsPerPage: 10
                }),                  
              
            }
            )
        };
    });
});
